package com.project.controller;

import com.project.bean.ShopCarBean;
import com.project.bean.UserBean;
import com.project.service.IShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 购物车controller
 */
@RestController
@RequestMapping("shop")
public class ShopController {
    @Autowired
    private IShopCarService shopCarService;

    @RequestMapping("buy")
    public String but(int productId, HttpServletRequest request){
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        shopCarService.add(user.getId(), productId);

        return "ok";
    }

    @RequestMapping("findByUser")
    public List<ShopCarBean>findByUser(HttpServletRequest request){
            UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        return shopCarService.findByUser(user.getId());
    }

    @RequestMapping("del")
    public String del(int id){
        shopCarService.del(id);
        return "ok";
    }
    @RequestMapping("update")
    public String update(int id,int num){
        shopCarService.update(id, num);
        return "ok";
    }
}
